package com.secsbrain.cmdb.biz.impl;

import java.util.Date;

import com.secsbrain.cmdb.biz.IHostService;
import com.secsbrain.sso.core.SsoHelper;
import com.secsbrain.cmdb.dal.mapper.HostBareMapper;
import com.secsbrain.cmdb.dal.model.Host;
import com.secsbrain.cmdb.dal.model.HostBare;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.secsbrain.cmdb.biz.IHostBareService;
import com.baomidou.framework.service.impl.SuperServiceImpl;
import com.baomidou.mybatisplus.plugins.Page;

/**
 *
 * HostBare 表数据服务层接口实现类
 *
 */
@Service
public class HostBareServiceImpl extends SuperServiceImpl<HostBareMapper, HostBare> implements IHostBareService {
	
	@Autowired
    IHostService hostService;

	/* (non-Javadoc)
	 * @see IHostBareService#queryHostBarePageList(com.baomidou.mybatisplus.plugins.Page, Host)
	 */
	@Override
	public Page<HostBare> queryHostBarePageList(Page<HostBare> pageBean, Host params) {
		pageBean.setRecords(this.baseMapper.queryHostBarePageList(pageBean, params));
		return pageBean;
	}

	@Override
	@Transactional
	public void insertHostBare(HostBare hostBare) {
		Host host=new Host();
		host.setAssetNumber(hostBare.getAssetNumber());
		host.setCabinetId(hostBare.getCabinetId());
		host.setClusterName(hostBare.getClusterName());
		host.setComment(hostBare.getComment());
		host.setCpuarch(hostBare.getCpuarch());
		host.setCpuModel(hostBare.getCpuModel());
		host.setCreateTime(new Date());
		host.setCreator(SsoHelper.currentUser().getNickname());
    	host.setMinionStatus(1);
		host.setDelStatus(0);
		host.setDiskCapacity(hostBare.getDiskCapacity());
		if(StringUtils.isEmpty(hostBare.getEnv())){
			host.setEnv("none");
		}else{
			host.setEnv(hostBare.getEnv());
		}
		host.setGroupName(hostBare.getGroupName());
		host.setHost(hostBare.getHost());
		host.setHostName(hostBare.getHostName());
		host.setIdcId(hostBare.getIdcId());
		host.setIp(hostBare.getIp());
		host.setIpPublic(hostBare.getIpPublic());
		host.setIstatus(hostBare.getIstatus());
		host.setKernel(hostBare.getKernel());
		host.setKernelRelease(hostBare.getKernelRelease());
		host.setManager(hostBare.getManager());
		host.setMemTotal(hostBare.getMemTotal());
		host.setNumCpus(hostBare.getNumCpus());
		host.setOs(hostBare.getOs());
		host.setOsarch(hostBare.getOsarch());
		host.setOsrelease(hostBare.getOsrelease());
		host.setPeriodDate(hostBare.getPeriodDate());
		host.setProductname(hostBare.getProductname());
		host.setPurchaseDate(hostBare.getPurchaseDate());
		host.setPurpose(hostBare.getPurpose());
		host.setSaltversion(hostBare.getSaltversion());
		host.setSystemSerialnumber(hostBare.getSystemSerialnumber());
		host.setType("physical");
		host.setUdesc(hostBare.getUdesc());
		host.setUposition(hostBare.getUposition());
		hostService.insertSelective(host);
		
		hostBare.setIhostId(host.getId());
		this.baseMapper.insertSelective(hostBare);
		
	}

	@Override
	@Transactional
	public void updateHostBare(HostBare hostBare) {
		Host host=new Host();
		host.setId(hostBare.getIhostId());
		host.setAssetNumber(hostBare.getAssetNumber());
		host.setCabinetId(hostBare.getCabinetId());
		host.setClusterName(hostBare.getClusterName());
		host.setComment(hostBare.getComment());
		host.setCpuarch(hostBare.getCpuarch());
		host.setCpuModel(hostBare.getCpuModel());
		host.setUpdateTime(new Date());
		host.setUpdator(SsoHelper.currentUser().getNickname());
		host.setDiskCapacity(hostBare.getDiskCapacity());
		host.setEnv(hostBare.getEnv());
		host.setGroupName(hostBare.getGroupName());
		host.setHost(hostBare.getHost());
		host.setHostName(hostBare.getHostName());
		host.setIdcId(hostBare.getIdcId());
		host.setIp(hostBare.getIp());
		host.setIpPublic(hostBare.getIpPublic());
		host.setIstatus(hostBare.getIstatus());
		host.setKernel(hostBare.getKernel());
		host.setKernelRelease(hostBare.getKernelRelease());
		host.setManager(hostBare.getManager());
		host.setMemTotal(hostBare.getMemTotal());
		host.setNumCpus(hostBare.getNumCpus());
		host.setOs(hostBare.getOs());
		host.setOsarch(hostBare.getOsarch());
		host.setOsrelease(hostBare.getOsrelease());
		host.setPeriodDate(hostBare.getPeriodDate());
		host.setProductname(hostBare.getProductname());
		host.setPurchaseDate(hostBare.getPurchaseDate());
		host.setPurpose(hostBare.getPurpose());
		host.setSaltversion(hostBare.getSaltversion());
		host.setSystemSerialnumber(hostBare.getSystemSerialnumber());
		host.setUdesc(hostBare.getUdesc());
		host.setUposition(hostBare.getUposition());
		hostService.updateSelectiveById(host);
		
		// hostbare不存在的情况
		int count=this.baseMapper.updateSelectiveById(hostBare);
		if(count<=0){
			this.baseMapper.insertSelective(hostBare);
		}
	}

	/* (non-Javadoc)
	 * @see IHostBareService#deleteHostBare(java.lang.Long)
	 */
	@Override
	@Transactional
	public void deleteHostBare(Long hostId) {
		Host hostUp=new Host();
		hostUp.setDelStatus(1);
		hostUp.setId(hostId);
		hostService.updateSelectiveById(hostUp);
		
		HostBare hostBareUp=new HostBare();
		hostBareUp.setIhostId(hostId);
		this.baseMapper.deleteSelective(hostBareUp);
	}

}